﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace SQL_Manager.OTServ
{
    public enum Order
    {
        Ascending = 0,
        Descending = 1
    }

    sealed public class Account_List
    {
        //TODO: Account_List exceptions, maybe? :D (feel so vulnerable with no checks :o)

        public List<Hashtable> _Data;
        private Database _Db;

        public int Limit = 1000;
        public int Offset = 0;
        public string Fields = "id";
        public string OrderBy = "id";

        #region Constructors

        //
        // Default Constructor #1
        // - This constructor is limited in the fact that you cannot set Fields or Order type
        //
        public Account_List(int Offset, int Limit, out List<Hashtable> AccountList)
        {
            this.Offset = Offset;
            this.Limit = Limit;
            Database.GetInstance(out this._Db);
            this.DoQuery(Order.Descending);
            AccountList = this._Data;
        }

        //
        // Default Constructor #2
        // - This constructor is limited in the fact that you cannot set the Order type
        //
        public Account_List(string Fields, int Offset, int Limit, out List<Hashtable> AccountList)
        {
            this.Fields = Fields;
            Database.GetInstance(out this._Db);
            this.DoQuery(Order.Descending);
            AccountList = this._Data;
        }

        //
        // Fully customizable constructor
        // - You don't set anything for this, it does not run the query off the bat. You must call DoQuery() and set all variables yourself
        //
        public Account_List()
        {
            Database.GetInstance(out this._Db);
        }

        #endregion

        #region Database-Related

        public void DoQuery(Order order)
        {
            this._Db.queryAll(string.Format("SELECT {0} FROM accounts ORDER BY {1} {2} LIMIT {3}, {4}", this.Fields, this.OrderBy, order == Order.Ascending ? "ASC" : "DESC", this.Offset, this.Limit), out this._Data);
        }

        #endregion
    }
}
